package com.example.dllo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.dllo.interface_.OnMyItemClick;
import com.example.dllo.entity.NewsList;
import com.example.dllo.farm_doctor.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 2015/12/4.
 */
public class Rich_News_Adapter extends RecyclerView.Adapter<Rich_News_Adapter.NewsHolder>{
    private List<NewsList.ListEntity> listEntities;
    private RequestQueue queue;
    private OnMyItemClick onMyItemClick;

    public void setOnMyItemClick(OnMyItemClick onMyItemClick) {
        this.onMyItemClick = onMyItemClick;
    }
    public Rich_News_Adapter(Context c){
        queue = Volley.newRequestQueue(c);
    }

    public void addData(List<NewsList.ListEntity> datas){
        listEntities = datas;
        notifyDataSetChanged();
    }

    @Override
    public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fill_news,null);

        return new NewsHolder(v);

    }



    @Override
    public void onBindViewHolder(NewsHolder holder, int position) {
        if (listEntities != null && listEntities.size() > 0) {
            NewsList.ListEntity entity = listEntities.get(position);
            holder.news_text_content.setText(entity.getTitle());
            String url = entity.getPicUrl();
                getBitmap(url, holder.news_image_content);
        }
        holder.itemposi = position;
    }

    private void getBitmap(String url, final ImageView imageTitle) {
        ImageRequest request = new ImageRequest(url,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        imageTitle.setImageBitmap(bitmap);

                    }
                }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {


            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.printStackTrace();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("Accept-Encoding","");
                return map;

            }
        };
        queue.add(request);
    }

    @Override
    public int getItemCount() {
        return listEntities != null && listEntities.size()> 0 ?listEntities.size():0;
    }


    public class NewsHolder extends RecyclerView.ViewHolder {

        private ImageView news_image_content;
        private TextView news_text_content;
        public int itemposi;

        public NewsHolder(View v) {
            super(v);

            news_image_content = (ImageView) itemView.findViewById(R.id.news_image_content);
            news_text_content = (TextView) itemView.findViewById(R.id.news_text_content);
        }
    }
//    @Override
//    public void onClick(View v) {
//        if (listEntities != null) {
//            AnswerList entity = listEntities.get(itemposi);
//            if (onMyItemClick != null) {
//                onMyItemClick.onMyItemClick(entity);
//            }
//
//        }
//
//    }

}

